<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	
	function getHierarchy($inArray, $pkField, $fkField, $fromTopLevelValue = 0) {
		$result = array();
        $ref = array();
        foreach ($inArray as $id => &$entry) {
            $ref[$entry[$pkField]] = &$entry;
            $entry['children'] = array();
            if ($entry[$fkField] == $fromTopLevelValue) {
                $result[$entry[$pkField]] = &$entry;
            } else {
                $ref[$entry[$fkField]]['children'][$entry[$pkField]] = &$entry;
            }
        }
        return $result;
	}
    
    function buildHierarchy($inArray, $itemClass, $linkClass, $level = 0) {
        $output = '';
        if ($level == 0)
            $start = PHP_EOL . '<ul class="navMenu" id="mainMenu">' . PHP_EOL;
        else
            $start = PHP_EOL . '<ul class="navMenuItem">' . PHP_EOL;
        foreach($inArray as $item)
        {
            if (isset($item['children'])) {
                $output .= '<li><a href="'. base_url() . $item['mnu_route'] .'">'. $item['mnu_label'] . '</a>' . buildHierarchy($item['children'], $itemClass, $linkClass, $level+1) . '</li>';
            } else {
                $output .= '<li><a href="'. base_url(). $item['mnu_route'] .'">'. $item['mnu_label'] . '</a></li>';
            }
        }
        return $output != ''? $start . $output . PHP_EOL . '</ul>':'';
    }
    
    function renderAuction($auction, $cssClassPrefix) {
        $content =  '<div id="auc-'. $auction['auc_id'] . '"' .' class="' . $cssClassPrefix .'-entry">';
        $content .=      '<div class="list-entry-photo">';
        $content .=              '<img src='. base_url() . $auction['auc_logopath'] . ' class="' .$cssClassPrefix .'-entry-logo"/>';
        $content .=      '</div>';
        $content .=         '<div class="list-entry-content">';
        $content .=              '<div class="list-entry-content-head">';
        $content .=                   heading($auction['auc_name'], 3);
        $content .=                   '<p class="aucdesc">' . $auction['auc_description'] . '</p>';
        $content .=              '</div>';
        $content .=              '<div class="list-entry-content-body">';
        $content .=                   '<div class="list-entry-content-left">';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                             '<div class="list-entry-content-group-left">' . 'Societate:' . '</div>';
        $content .=                             '<div class="list-entry-content-group-right">' . ($auction['owner']!=FALSE?$auction['owner']['cnt_firstname']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                            '<div class="list-entry-content-group-left">' . 'CIF:' . '</div>';
        $content .=                            '<div class="list-entry-content-group-right">' . ($auction['owner']!=FALSE?$auction['owner']['cnt_regid']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                             '<div class="list-entry-content-group-left">' . 'RC:' . '</div>';
        $content .=                             '<div class="list-entry-content-group-right">' . ($auction['owner']!=FALSE?$auction['owner']['cnt_traderegid']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                   '</div>';
        $content .=                   '<div class="list-entry-content-right">';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                             '<div class="list-entry-content-group-left">' . 'Persoana de contact:' . '</div>';
        $content .=                             '<div class="list-entry-content-group-right">' . ($auction['contact']!=FALSE?$auction['contact']['cnt_firstname'] .' ' . $auction['contact']['cnt_lastname']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                            '<div class="list-entry-content-group-left">' . 'Telefon:' . '</div>';
        $content .=                            '<div class="list-entry-content-group-right">' . ($auction['contact']!=FALSE?$auction['contact']['cnt_phonenb']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                             '<div class="list-entry-content-group-left">' . 'Email:' . '</div>';
        $content .=                             '<div class="list-entry-content-group-right">' . ($auction['contact']!=FALSE?$auction['contact']['cnt_email']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                   '</div>';
        $content .=              '</div>';
        $content .=              '<div class="list-entry-content-footer">';
        $content .=              '</div>';
        $content .=         '</div>';
        $content .=  '</div>';
        return $content;
    }
    
    function renderAuctionListEntry($auction, $cssClassPrefix) {
        $content = '';
        if ($auction['detail_page'] != FALSE) {
            $content .=  '<a href='. base_url() .'site/licitatie/' . $auction['detail_page']['pag_name'] . '>'; 
        }
        $content .= renderAuction($auction, $cssClassPrefix);
        if ($auction['detail_page']) {
            $content .=  '</a>';
        }
        return $content;
    }
    
    function renderDocument($document, $cssClassPrefix) {
        $content = '<div id="doc-' . $document['doc_id'] .'" class="' . $cssClassPrefix .'-entry">';
        $content .=         '<div class="list-entry-photo">';
        $content .=              '<a href=' .base_url() . $document['doc_path'] . '>'; 
        //$content .=                   '<img src='. base_url() . 'media/doc.png' . ' class="' .$cssClassPrefix .'-entry-logo"/>';
        $content .=              '</a>';
        $content .=         '</div>';
        $content .=         '<div class="list-entry-content">';
        $content .=              '<div class="list-entry-content-head">';
        $content .=              '<a href=' .base_url() . $document['doc_path'] . ' target="_blank">'; 
        $content .=                   heading($document['doc_name'], 3);
        $content .=              '</a>';
        $content .=                   '<p class="aucdesc">' . $document['doc_description'] . '</p>';
        $content .=              '</div>';
        $content .=              '<div class="list-entry-content-body">';
        $content .=                   '<div class="list-entry-content-left">';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                             '<div class="list-entry-content-group-left">' . 'Societate:' . '</div>';
        $content .=                             '<div class="list-entry-content-group-right">' . ($document['owner']!=FALSE?$document['owner']['cnt_firstname']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                            '<div class="list-entry-content-group-left">' . 'CIF:' . '</div>';
        $content .=                            '<div class="list-entry-content-group-right">' . ($document['owner']!=FALSE?$document['owner']['cnt_regid']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                             '<div class="list-entry-content-group-left">' . 'RC:' . '</div>';
        $content .=                             '<div class="list-entry-content-group-right">' . ($document['owner']!=FALSE?$document['owner']['cnt_traderegid']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                   '</div>';
        $content .=                   '<div class="list-entry-content-right">';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                             '<div class="list-entry-content-group-left">' . 'Persoana de contact:' . '</div>';
        $content .=                             '<div class="list-entry-content-group-right">' . ($document['contact']!=FALSE?$document['contact']['cnt_firstname'] .' ' . $document['contact']['cnt_lastname']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                            '<div class="list-entry-content-group-left">' . 'Telefon:' . '</div>';
        $content .=                            '<div class="list-entry-content-group-right">' . ($document['contact']!=FALSE?$document['contact']['cnt_phonenb']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                             '<div class="list-entry-content-group-left">' . 'Email:' . '</div>';
        $content .=                             '<div class="list-entry-content-group-right">' . ($document['contact']!=FALSE?$document['contact']['cnt_email']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                   '</div>';
        $content .=              '</div>';
        $content .=              '<div class="list-entry-content-footer">';
        $content .=              '</div>';
        $content .=         '</div>';
        $content .= '</div>';
        return $content;
    }
    
    function renderAuctionNewsEntry($item, $cssClassPrefix) {
        $auction = $item['auction'];
        $content =  '<div id="auc-'. $item['new_id'] . '"' .' class="' . $cssClassPrefix .'-entry">';
        $content .=      '<div class="list-entry-photo">';
        $content .=           '<a href=' .base_url() . 'site/licitatie/' . $auction['page']['pag_name'] . '>'; 
        $content .=              '<img src='. base_url() . $item['auction']['auc_logopath'] . ' class="' .$cssClassPrefix .'-entry-logo"/>';
        $content .=           '</a>';
        $content .=      '</div>';
        $content .=         '<div class="list-entry-content">';
        $content .=              '<div class="list-entry-content-head">';
        $content .=                   heading($item['new_title'], 3);
        $content .=                   '<p class="aucdesc">' . $auction['auc_description'] . '</p>';
        $content .=              '</div>';
        $content .=              '<div class="list-entry-content-body">';
        $content .=                   '<div class="list-entry-content-left">';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                             '<div class="list-entry-content-group-left">' . 'Societate:' . '</div>';
        $content .=                             '<div class="list-entry-content-group-right">' . ($auction['owner']!=FALSE?$auction['owner']['cnt_firstname']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                            '<div class="list-entry-content-group-left">' . 'CIF:' . '</div>';
        $content .=                            '<div class="list-entry-content-group-right">' . ($auction['owner']!=FALSE?$auction['owner']['cnt_regid']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                             '<div class="list-entry-content-group-left">' . 'RC:' . '</div>';
        $content .=                             '<div class="list-entry-content-group-right">' . ($auction['owner']!=FALSE?$auction['owner']['cnt_traderegid']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                   '</div>';
        $content .=                   '<div class="list-entry-content-right">';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                             '<div class="list-entry-content-group-left">' . 'Persoana de contact:' . '</div>';
        $content .=                             '<div class="list-entry-content-group-right">' . ($auction['contact']!=FALSE?$auction['contact']['cnt_firstname'] .' ' . $auction['contact']['cnt_lastname']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                            '<div class="list-entry-content-group-left">' . 'Telefon:' . '</div>';
        $content .=                            '<div class="list-entry-content-group-right">' . ($auction['contact']!=FALSE?$auction['contact']['cnt_phonenb']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                             '<div class="list-entry-content-group-left">' . 'Email:' . '</div>';
        $content .=                             '<div class="list-entry-content-group-right">' . ($auction['contact']!=FALSE?$auction['contact']['cnt_email']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                   '</div>';
        $content .=              '</div>';
        $content .=              '<div class="list-entry-content-footer">';
        $content .=              '</div>';
        $content .=         '</div>';
        $content .=  '</div>';
        return $content;
    }
    
    function renderDocumentNewsEntry($item, $cssClassPrefix) {
        $document = $item['document'];
        $content =   '<div id="news-item-'. $item['new_id'] . '"' .' class="' . $cssClassPrefix .'-entry">';
        $content .=         '<div class="list-entry-photo">';
        $content .=              '<a href=' .base_url() . $document['doc_path'] . ' rel="_blank">'; 
        $content .=                   '<img src='. base_url() . 'media/doc.png' . ' class="' .$cssClassPrefix .'-entry-logo"/>';
        $content .=              '</a>';
        $content .=         '</div>';
        $content .=         '<div class="list-entry-content">';
        $content .=              '<div class="list-entry-content-head">';
        $content .=                   heading($item['new_title'], 3);
        $content .=                   '<p class="aucdesc">' . $document['doc_description'] . '</p>';
        $content .=              '</div>';
        $content .=              '<div class="list-entry-content-body">';
        $content .=                   '<div class="list-entry-content-left">';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                             '<div class="list-entry-content-group-left">' . 'Societate:' . '</div>';
        $content .=                             '<div class="list-entry-content-group-right">' . ($document['owner']!=FALSE?$document['owner']['cnt_firstname']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                            '<div class="list-entry-content-group-left">' . 'CIF:' . '</div>';
        $content .=                            '<div class="list-entry-content-group-right">' . ($document['owner']!=FALSE?$document['owner']['cnt_regid']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                             '<div class="list-entry-content-group-left">' . 'RC:' . '</div>';
        $content .=                             '<div class="list-entry-content-group-right">' . ($document['owner']!=FALSE?$document['owner']['cnt_traderegid']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                   '</div>';
        $content .=                   '<div class="list-entry-content-right">';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                             '<div class="list-entry-content-group-left">' . 'Persoana de contact:' . '</div>';
        $content .=                             '<div class="list-entry-content-group-right">' . ($document['contact']!=FALSE?$document['contact']['cnt_firstname'] .' ' . $document['contact']['cnt_lastname']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                            '<div class="list-entry-content-group-left">' . 'Telefon:' . '</div>';
        $content .=                            '<div class="list-entry-content-group-right">' . ($document['contact']!=FALSE?$document['contact']['cnt_phonenb']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                        '<div class="list-entry-content-group">';
        $content .=                             '<div class="list-entry-content-group-left">' . 'Email:' . '</div>';
        $content .=                             '<div class="list-entry-content-group-right">' . ($document['contact']!=FALSE?$document['contact']['cnt_email']:'') . '</div>';
        $content .=                        '</div>';
        $content .=                   '</div>';
        $content .=              '</div>';
        $content .=              '<div class="list-entry-content-footer">';
        $content .=              '</div>';
        $content .=         '</div>';
        $content .=  '</div>';
        return $content;
    }
    
    function renderNews($item, $cssClassPrefix) {
        $content = '';
        if ($item['new_type'] == 'L') {
            $content .= renderAuctionNewsEntry($item, $cssClassPrefix);
        } else {
             $content .= renderDocumentNewsEntry($item, $cssClassPrefix);
        }
        return $content;
    }
    
    function renderDocumentListEntry($document, $cssClassPrefix) {
        $content = '';
        $content .= renderDocument($document, $cssClassPrefix);
        return $content;
    }
    
	
	function _required($required, $data) {
		foreach($required as $field) 
			if(!isset($data[$field])) 
				return false;
    	return true;
	}
	
	function _default($defaults, $options)
	{
	    return array_merge($defaults, $options);
	}
